/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import local.RoleFacadeLocal;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Role;

/**
 *
 * @author karennyq
 */
@Stateless
public class RoleFacade implements RoleFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public Collection findAllRoles() {
        Query query = em.createQuery("SELECT r FROM Role r");
        return (Collection) query.getResultList();
    }

    @Override
    public Role findRole(Long role_id) {
        Role r = em.find(Role.class, role_id);
        return r;
    }

    @Override
    public Role findRole(String role_name) {
        Query query = em.createQuery("SELECT r FROM Role r WHERE r.role_name=?1");
        query.setParameter(1, role_name);
        Role r = (Role) query.getSingleResult();

        return r;
    }
}
